-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rust: Include parameters and patterns in the CFG #17686
Conversation
da0bcd6
to
9ed1394
Compare
9ed1394
to
756affa
Compare
@@ -117,13 +117,61 @@ class BooleanCompletion extends ConditionalCompletion, TBooleanCompletion { | |||
override string toString() { result = "boolean(" + value + ")" } | |||
} | |||
|
|||
/** Holds if node `pat` has the constant match value `value`. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Rust use the term irrefutable
pattern for a pattern that cannot fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is correct use of "irrefutable". Irrefutable is property of a pattern, but this predicate is a property of a pattern as an AST node and considers the surrounding AST context as well.
For instance, in
match b { Some(a) => 1, None => 0 }
this predicate holds for None
(since the last pattern in a match
arm is guaranteed to succeed due to exhaustiveness) but None
is not an irrefutable pattern.
I think we should rename this predicate (not sure to what though). We could also extract a proper isIrrefutablePattern
predicate from this predicate (but it will probably be a conservative approximation as refutability for custom types depends on the type definition. E.g. the pattern Foo(a)
is irrefutable if Foo
the only case in the enum
and refutable otherwise).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you're right. Perhaps isExhaustiveMatch
or similar. An irrefutable pattern is exhaustive on its own, and a last pattern in match
should be exhaustive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like a great name to me. I'll do a PR :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, PR is here #17751
/** Holds if node `pat` has the constant match value `value`. */ | ||
pragma[nomagic] | ||
private predicate isMatchConstant(Pat pat, boolean value) { | ||
value = true and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps remove the value
and it seems to be always true
// parameter patterns must be exhaustive | ||
pat = any(Param p).getPat() | ||
) and | ||
not pat = any(ForExpr for).getPat() // workaround until `for` loops are desugared |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand why you exclude patterns in for
here, they should also never fail
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats because of how we currently model for
loops; the pattern is used to represent the condition.
@@ -141,11 +138,10 @@ class LogicalAndTree extends PostOrderTree, LogicalAndExpr { | |||
|
|||
class BlockExprTree extends StandardPostOrderTree, BlockExpr { | |||
override AstNode getChildNode(int i) { | |||
result = super.getStmtList().getStatement(i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change? I see inconsistent use of instanceof SomeClass
and extends ..., SomeClass
in this file. Wouldn't it be better to consistently use instanceof
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to consistently use extends
.
// Closures have their own CFG scope, so we need to make sure that their | ||
// CFG is not mixed with the surrounding CFG. This is done by retrofitting | ||
// `first`, `propagatesAbnormal`, and `succ` below. | ||
class ClosureExprTree extends StandardPostOrderTree, ClosureExpr { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I like the way this is formulated. If I understand correctly you want the succ
relation of a StandardOrderTree
and the first
and last
predicates of a LeafTree
. Perhaps it would be clearer to make this class extend StandardOrderTree
and avoid the not succ = this
restriction in succ
and add an explicit last
predicate. It's about the same amount of lines and you avoid the tweaks to "undo" the behaviour of a Post
order tree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, I had completely forgotten about StandardTree
.
* Provides `ControlFlowTree`s for patterns. | ||
* | ||
* Since patterns destruct values, they are modeled in pre-order, except for | ||
* `OrPat`s and `IdentPat`s. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are ident and or-patterns different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because in e @ pat
we only "evalute" e
when pat
matches. And in pat1 | pat2
we need it to be post-order in order to get the correct dominance information needed for guards.
|
||
class LiteralPatTree extends LeafTree, LiteralPat { } | ||
|
||
class MacroPatTree extends LeafTree, MacroPat { } // todo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, in another PR ;-)
override Pat getPat(int i) { result = this.getField(i) } | ||
} | ||
|
||
class ConstBlockPatTree extends LeafTree, ConstBlockPat { } // todo? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the constant value inside is evaluated compile-time, so perhaps having this as a LeafTree
is the right thing to do.
This PR adds parameters and patterns to the CFG, needed for SSA. Unlike expressions, which construct values, patterns destruct values, so they are modeled pre-order in the CFG. The first exception being identifier patterns
where we need to first match against
Option::None
and only bindx
in case of a match, and the second exception being|
patterns, which are modeled like logical disjunction expressions.Examples
Simple parameters
Before
After
Match expression
Before
After
Parameter pattern matching
Before
After